<?php
/**
 * 把行为扩展放入项目的Lib\Behavior\目录下面
 * 然后使用B('行为名称')的方式在程序中调用。
 B('WapCheck');
 * @author xinqiyang
 *
 */
class WapCheckBehavior extends Behavior
{
	public function run()
	{
		$wapUrl = C('WAP_REDIRECT_URL') || '/wap';
		if (C('WAP_REDIRECT_ON') && self::isWap())
		{
			header('Location:' . $wapUrl);
			exit;
		}
	}
	
	static private function isWap()
	{
		$ua = strtolower($_SERVER['HTTP_USER_AGENT']);
		$uachar = "/(nokia|sony|ericsson|mot|samsung|sgh|lg|sie|philips|panasonic|alcatel|lenovo|cldc|midp|wap|mobile)/i";
		if (($ua == '' || preg_match($uachar, $ua)) && !strpos(strtolower($_SERVER['REQUEST_URI']), 'wap'))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
}

?>